@echo off

set FBC=fbc.exe

set INCDIR=src\inc
set SRCDIR=src

if "%FBC_CFLAGS%"=="" (
set FBC_CFLAGS=-c -w all -m main -i %INCDIR%
) ELSE (
set FBC_CFLAGS=%FBC_CFLAGS% -c -w all -m main -i %INCDIR%
)
if "%FBC_LFLAGS%"=="" (
set FBC_LFLAGS= -s console -m main -x weld weld.rc
) ELSE (
set FBC_LFLAGS=%FBC_LFLAGS% -s console -m main -x weld weld.rc
)
if "%1"=="weld" goto buildit:
if "%1"=="clean" goto clean:
if "%1"=="usage" goto usage:
goto usage:

:buildit
	echo on
	
	:: compile source files
	@for /R %%s in (%SRCDIR%\*.bas) do %FBC% %FBC_CFLAGS% "%%s"
	
	:: link object files
	%FBC% %FBC_LFLAGS% "%SRCDIR%\*.o"	
	@goto end:

:clean
	echo on
	del /Q "weld.exe"  2> NUL
	del /Q "%SRCDIR%\*.o" 2> NUL
	@goto end:

:usage
	@echo off
	echo "usage: BUILD.BAT [weld|clean]"
	echo.
	echo    Running BUILD.BAT with no commandline arguments displays this help
	echo    text. One of the following options can be specified:
	echo.
	echo    lib              - builds the weld executable.
	echo    clean            - removes all compiled files.
	echo.
	echo	Compilation can be effected by the environment variables:
	echo	FBC_CFLAGS: defaults to: -c -w all -m main -i %INCDIR%
	echo		you call set FBC_CFLAGS=-i dir/to/ext/inc for example but defaults
	echo		are still there.
	echo.
	echo	FBC_LFLAGS: defaults to: -s console -m main -x weld weld.rc
	echo		you can add for example -i dir/to/ext/lib but defaults stand.
	echo.

:end
